var mysql  = require('mysql');
const { mysql: config } = require('../config')
exports.query = function (sql) {//数据库查询
  var promise = new Promise(function (resolve, reject) {
    var pool = mysql.createPool({
        host     :config.host,
        user     : config.user,
        password :config.pass,
        port: config.port,
        database: config.db,
    });
      pool.getConnection(function(err,conn){
        if(err){
            callback(err,null,null);
        }else{
            conn.query(sql,[1],function(err,results,fields){
                //释放连接
                conn.release();
                //事件驱动回调
                resolve(results);
            });
        }
    });
  });
  promise.then(function (value) {
    console.log(value);
    return value;
    // success
  }, function (value) {
      return value;
    // failure
  });
  return promise;
};

exports.insert = function (sql,sqlParams) {//数据库插入
    var promise = new Promise(function (resolve, reject) {
        var pool = mysql.createPool({
            host     :config.host,
            user     : config.user,
            password :config.pass,
            port: config.port,
            database: config.db,
        });
        pool.getConnection(function(err,conn){
            if(err){
                callback(err,null,null);
            }else{

                conn.query(sql,[sqlParams],function (err, result) {
                    if(err){
                        result='[INSERT ERROR] - '+err.message;
                    }
                    console.log('--------------------------INSERT----------------------------');
                    console.log('INSERT ID:',result);
                    console.log('-----------------------------------------------------------------\n\n');
                    conn.release();
                    resolve(result)
                });
            }
        });
    });
    promise.then(function (value) {
        console.log(value);
        return value;
        // success
    }, function (value) {
        return value;
        // failure
    });
    return promise;
};
exports.delete = function (delSql) {//数据库删除
    var promise = new Promise(function (resolve, reject) {
        var pool = mysql.createPool({
            host     :config.host,
            user     : config.user,
            password :config.pass,
            port: config.port,
            database: config.db,
        });
        pool.getConnection(function(err,conn){
            if(err){
                callback(err,null,null);
            }else{
                conn.query(delSql,function (err, result) {
                    if(err){
                        console.log('[DELETE ERROR] - ',err.message);
                        return;
                    }
                    conn.release();
                    //事件驱动回调
                    resolve(result);
                });
            }
        });
    });
    promise.then(function (value) {
        console.log(value);
        return value;
        // success
    }, function (value) {
        return value;
        // failure
    });
    return promise;
};

